package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.LabelInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.LabelInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理模块")
@RestController
@RequestMapping("admin/label")
public class LabelController {
    @Autowired
    private LabelInfoService labelInfoService;

    @GetMapping("list")
    @Operation(summary = "查询所有标签")
    public Result<List<LabelInfo>> list(
            @Parameter(description = "类型:1-公寓,2-房屋",in = ParameterIn.QUERY,required = false)
            @RequestParam(value = "type",required = false)
            ItemTypeEnum type
    ) {
        LambdaQueryWrapper<LabelInfo> queryWrapper = Wrappers.lambdaQuery(LabelInfo.class);
        queryWrapper.eq(type!=null,LabelInfo::getType,type);
        return Result.ok(labelInfoService.list(queryWrapper));
    }

    @PostMapping("saveOrUpdate")
    @Operation(summary = "2、新增或更新标签")
    public Result saveOrUpdate(
            @RequestBody LabelInfo labelInfo
    ){
        labelInfoService.saveOrUpdate(labelInfo);
        return Result.ok();
    }

    //    /admin/label/deleteById?id=4
    @DeleteMapping("deleteById")
    @Operation(summary = "3、根据id删除标签")
    public Result saveOrUpdate(@Parameter(description = "主键值")
                               @RequestParam(value = "id",required = true)Long id){

        return Result.ok(labelInfoService.removeById(id));
    }



}
